package com.maple.blog.controller;

import com.maple.blog.bean.ArticleBean;
import com.maple.blog.bean.JianshuBean;
import com.maple.blog.bean.ResponseBean;
import com.maple.blog.controller.service.JianshuService;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.ResponseBody;

import java.beans.ConstructorProperties;

/**
 * 描述: com.maple.blog.controller
 *
 * @author hz.lei
 * @date 2018年04月27日 下午11:33
 */
@Controller
public class BlogController {
    private final JianshuService jianshuService;

    @GetMapping({"/"})
    public String home() {
        return "redirect:/blog";
    }

    @GetMapping({"/p/{articleId}"})
    public String article(@PathVariable String articleId, Model model) {
        ArticleBean article = this.jianshuService.parseArticle(articleId);
        article.setArticleId(articleId);
        model.addAttribute("article", article);
        return "article";
    }

    @GetMapping({"/blog"})
    public String blog(Model model) {
        JianshuBean jianshuBean = this.jianshuService.parseUserAndBlogs();
        model.addAttribute("jianshu", jianshuBean);
        return "blog";
    }

    @GetMapping({"/blog/{page}"})
    @ResponseBody
    public ResponseBean blog(@PathVariable("page") int page) {
        JianshuBean jianshuBean = this.jianshuService.parseArticleList(page);
        return ResponseBean.success(jianshuBean);
    }

    @GetMapping({"/nb/{id}"})
    public String notebook(@PathVariable("id") String id, Model model) {
        JianshuBean jianshuBean = this.jianshuService.parseNoteBooks(id);
        model.addAttribute("jianshu", jianshuBean);
        return "blog";
    }

    @GetMapping({"/nb/{id}/{page}"})
    @ResponseBody
    public ResponseBean notebook(@PathVariable("id") String id, @PathVariable("page") int page) {
        JianshuBean jianshuBean = this.jianshuService.parseArticleListInNotebook(id, page);
        return ResponseBean.success(jianshuBean);
    }

    @GetMapping({"/nbs/{page}"})
    @ResponseBody
    public ResponseBean notebooks(@PathVariable("page") int page) {
        JianshuBean jianshuBean = this.jianshuService.parseNotebookList(page);
        return ResponseBean.success(jianshuBean);
    }

    @GetMapping({"/c/{id}"})
    public String collection(@PathVariable("id") String id, Model model) {
        JianshuBean jianshuBean = this.jianshuService.parseCollections(id);
        model.addAttribute("jianshu", jianshuBean);
        return "blog";
    }

    @GetMapping({"/c/{id}/{page}"})
    @ResponseBody
    public ResponseBean collection(@PathVariable("id") String id, @PathVariable("page") int page) {
        JianshuBean jianshuBean = this.jianshuService.parseArticleListInCollection(id, page);
        return ResponseBean.success(jianshuBean);
    }

    @GetMapping({"/cs/{page}"})
    @ResponseBody
    public ResponseBean collections(@PathVariable("page") int page) {
        JianshuBean jianshuBean = this.jianshuService.parseCollectionList(page);
        return ResponseBean.success(jianshuBean);
    }

    @ConstructorProperties({"jianshuService"})
    public BlogController(final JianshuService jianshuService) {
        this.jianshuService = jianshuService;
    }

}
